﻿using System;
using fastCSharp;

namespace diantou.web.ajax.admin
{
    /// <summary>
    /// 用户调用
    /// </summary>
    [fastCSharp.code.cSharp.ajax(IsPool = true)]
    class user : adminHandle<user>
    {
        /// <summary>
        /// 模拟用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isChangeVisit"></param>
        /// <returns></returns>
        public bool Simulation(int userId, bool isChangeVisit)
        {
            diantou.dataAccess.user user = diantou.dataAccess.clientCache.User[userId];
            if (user != null)
            {
                SetCookie(new fastCSharp.net.tcp.http.cookie("SimulationUserId", (isChangeVisit ? userId : -userId).toString(), date.NowSecond.AddDays(1), diantou.dataModel.path.domain.Default.Domain, "/", false, false));
                return true;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isHttpServer"></param>
        /// <param name="isDataServer"></param>
        /// <returns></returns>
        public bool MemoryGraph(bool isHttpServer, bool isExamDataServer, bool isDataServer)
        {
            if (isDataServer) diantou.dataAccess.tcpCall.memory.memoryGraph();
            if (isExamDataServer) diantou.examDataAccess.tcpCall.memory.memoryGraph();
            if (isHttpServer) fastCSharp.memoryGraph.SaveFile(@"memoryGraph_" + date.Now.ToString("yyyyMMdd_HHmmss") + ".dat");
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isHttpServer"></param>
        /// <param name="isDataServer"></param>
        public void GCCollect(bool isHttpServer, bool isExamDataServer, bool isDataServer)
        {
            if (isDataServer) diantou.dataAccess.tcpCall.memory.gc();
            if (isExamDataServer) diantou.examDataAccess.tcpCall.memory.gc();
            if (isHttpServer) GC.Collect();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isHttpServer"></param>
        /// <param name="isDataServer"></param>
        public void ThreadLog(bool isHttpServer, bool isExamDataServer, bool isDataServer)
        {
            if (isDataServer) diantou.dataAccess.tcpCall.memory.threadLog();
            if (isExamDataServer) diantou.examDataAccess.tcpCall.memory.threadLog();
            if (isHttpServer) fastCSharp.threading.threadPool.CheckLog();
        }
        /// <summary>
        /// 重启数据服务
        /// </summary>
        /// <returns></returns>
        public bool ResetDataServer(bool isExam)
        {
            if (isExam) diantou.examDataAccess.tcpCall.dataAccessTcpCallVerify.reset();
            else diantou.dataAccess.tcpCall.dataAccessTcpCallVerify.reset();
            return true;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public diantou.dataModel.user Get(int userId)
        {
            return diantou.dataAccess.clientCache.User[userId];
        }
        /// <summary>
        /// 修改用户权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="permission"></param>
        /// <param name="onReturn"></param>
        public void ReworkPermission(int userId, diantou.dataModel.user.permission permission, diantou.dataModel.user.type type, string contestRemark, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.reworkPermission(userId, permission, type, contestRemark, onReturn);
        }
        /// <summary>
        /// 获取邮箱列表
        /// </summary>
        /// <param name="onReturn"></param>
        public void GetEmails(int[] userIds, bool isProgrammer, bool isEnterprise, Action<fastCSharp.net.returnValue<diantou.dataModel.user[]>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.getEmails(userIds, isProgrammer, isEnterprise, onReturn);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public void SendEmail(int userId, string title, string content, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.sendEmail(userId, title, content, onReturn);
        }
        /// <summary>
        /// 增加点头盾
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="point"></param>
        /// <param name="onReturn"></param>
        public void AddNodPoint(int[] userIds, int point, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.addNodPoint(userIds, point, currentUser.Id, onReturn);
        }
        /// <summary>
        /// 设置长连接轮询队列最大线程数量
        /// </summary>
        /// <param name="threadCount"></param>
        /// <param name="onReturn"></param>
        public void SetPollQueueTaskMaxThreadCount(int threadCount, bool isExam, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            if (isExam) diantou.examDataAccess.tcpCall.enterprisePoll.setQueueTaskMaxThreadCount(threadCount, onReturn);
            else diantou.dataAccess.tcpCall.poll.setQueueTaskMaxThreadCount(threadCount, onReturn);
        }
        /// <summary>
        /// 未验证用户邮件群发
        /// </summary>
        /// <returns></returns>
        public bool RegisterVerifyEmail()
        {
            return diantou.dataAccess.tcpCall.user.registerVerifyEmail();
        }
        /// <summary>
        /// 生成用户微信绑定二维码
        /// </summary>
        /// <param name="userId">-1表示所有用户</param>
        /// <param name="isOver">是否覆盖已有图片</param>
        public void CreateWeixinQrCode(int userId, bool isOver)
        {
            diantou.dataAccess.tcpCall.user.CreateWeixinQrCode(userId, isOver);
        }
        /// <summary>
        /// 修改用户类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <param name="onReturn"></param>
        public void SetType(int userId, diantou.dataModel.user.type type, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            diantou.dataAccess.tcpCall.user.reworkType(userId, type, onReturn);
        }
        /// <summary>
        /// 企业账户充值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        public void EnterpriseRecharge(int userId, int amount, Action<fastCSharp.net.returnValue<int>> onReturn)
        {
            diantou.examDataAccess.tcpCall.enterprise.recharge(userId, amount, onReturn);
        }
    }
}
